<template>
	<view class="">
		
		<view v-if="need_pay == 1">
			<view class="head">
				<view @tap="openyaoqing(0, 'center')" class="headtext">邀约新的商家</view>
			</view>
			
			<!-- 商家列表 -->
			<admin-details-fac-list @factory_list='factory_list' :factory_list_data="factory_list_data" :activity_id="this.activity_id" :scrollH="scrollH" :factory_id="factory_id" :status="status" />
		</view>
		
		<view v-else-if="need_pay == 0">
			<view v-show="admintabShow">
				<view class="head">
					<view @tap="openyaoqing(0, 'center')" class="headtext">邀约新的商家</view>
				</view>
				
				<!-- 商家列表 -->
				<admin-details-fac-list @factory_list='factory_list' :factory_list_data="factory_list_data" :activity_id="this.activity_id" :scrollH="scrollH" :factory_id="factory_id" :status="status" />
			</view>
			
			<view v-show="clienttapShow">
				<view class="clienttext" style="">*说明：客户直接进入报名页面报名的，没有员工归属的客户则需管理员在此页面审核客户！</view>
				<view class="staff">
					<view class="staff-em"></view>
					<view class="staff-text">客户申请列表</view>
				</view>
				<block v-for="(item, index) in client_list" :key="index">
					<view class="pl-3 pr-3 mt-1">
						<view class="d-flex a-center j-sb">
							<view class="d-flex a-center ">
								<view style="width: 400rpx; margin-right: 30rpx;">
									<view><span style="color: #777;">姓名：</span>{{ item.name }}</view>
									<view><span style="color: #777;">电话：</span>{{ item.tel }}</view>
								</view>
							</view>
							<view @click="tongguo(index, item.id)" :class="item.is_check == 0 ? 'no-pass' : 'pass'">
								{{ item.is_check ? '已审核' : '点击审核' }}
							</view>
						</view>
					</view>
					<divider />
				</block>
				<loadmore :status="clientstatus"></loadmore>
			</view>
			
			<view class="bottom-box">
				<view @tap="admintab" class="admin-box">
					<image :src="adminicon" class="admin-img"></image>
					<text :style="admintabShow == true ? 'color: #475dfc' : ''">邀约商家</text>
				</view>
				<view @tap="clienttab" class="admin-box">
					<image :src="clienticon" class="admin-img"></image>
					<text :style="clienttapShow == true ? 'color: #475dfc' : ''" >客户审核</text>
				</view>
			</view>
		</view>
		
		<view class="top" :style="{'display':(flag===true? 'block':'none')}">
			<uni-icons class="topc" type="arrowthinup" size="30" @click="top"></uni-icons>
		</view>

		<canvas class="canvas" canvas-id="qrcode" />
		<openAlert :BgColor="BgColor" closeBtn ref="openAlertyaoqing" :AlertClass="AlertClass" :AlertPosition="AlertPosition">
			<view class="d-flex flex-column a-center">
				<text class="sharetext">长按图片,分享好友</text>
				<image :src="haibao_url" class="shareimg" />
			</view>
		</openAlert>

	</view>
</template>

<script>
	import uQRCode from '@/common/uqrcode.js';
	import adminTabBar from '../../components/admin/admint_tabbar.vue'
	import adminDetailsFacList from '@/components/admin/admin_details_faclist.vue'
	import uniIcons from '../../components/common/uni-icons/uni-icons.vue'
	import loadmore from '../../components/common/uni-load-more.vue'
	require('@/components/poster/poster_maker.js');

	export default {
		components: {
			adminDetailsFacList,
			adminTabBar,
			uniIcons,
			loadmore,
		},
		data() {
			return {
				scrollH: 100,
				BgColor: '#838383',
				AlertClass: 0,
				AlertPosition: '',
				factory_list_data: [],
				haibao_url: "",
				url: '',
				activity_name: '',
				// company_name: '',
				activity_id: 0,
				factory_id: 0,
				page: 1,
				flag: false,
				status: 'more',
				adminstatus: 'more',
				clientstatus: 'more',
				haibao_id: 0,
				sharemaintitle: '',
				sharetitle: '',
				shareimage: '',
				need_pay: -1,
				clienticon: '../../static/client.png',
				adminicon: '../../static/admin1.png',
				admintabShow: true,
				clienttapShow: false,
				client_list: [],
			};
		},
		onLoad(e) {
			this.activity_id = e.activity_id
			this.need_pay = e.need_pay
			this.$comm.appLogin(user_info=>{
				this.get_invite_check_list()
				this.factory_list(this.activity_id);
				this.set_config(this.activity_id)
			})
		},
		onShow() {
			// this.loaddata()
		},
		onPullDownRefresh() {
			this.loaddata()
		},
		onReachBottom() {
			if(this.status == 'nomore') return
			this.page++
			if (this.admintabShow == true) {
				this.factory_list()
			} else if (this.clienttapShow == true) {
				this.get_invite_check_list()
			}
			
		},
		methods: {
			tongguo(index, id) {
				if (this.client_list[index].is_check == 1) {
					uni.showToast({
						title: '已审核，请勿重复点击！',
						icon: 'none',
						duration: 2000,
					})
					return
				}
				this.$comm.request({
					url: 'api/invite_check',
					method: 'POST',
					data: {
						check: 1, 
						id: id,
					},
					success: (res) => {
						console.log(res)
						if (res.data.code == 200) {
							this.$set(this.client_list[index],'is_check',1)
							uni.showToast({
								title: '审核成功！',
								icon: 'success',
								duration: 2000,
							})
						}
						this.get_invite_check_list()
					}
				})
			},
			
			get_invite_check_list() {
				if (this.clientstatus == 'nomore') return
				this.$comm.request({
					url: 'api/invite_check_list',
					method: 'GET',
					data: {
						activity_id: this.activity_id,
						page: this.page,
					},
					success: (res) => {
						console.log(res,'get_invite_check_list')
						let list = res.data.result.list
						// console.log(list,9999)
						this.client_list = this.client_list.concat(list)
						if (list.length < 10) {
							this.clientstatus = 'nomore'
						}
					}
				})
			},
			
			//分享链接
			share() {
				let maintitle = this.sharemaintitle
				let content = this.sharetitle
				let shareimg = this.shareimage
				if (maintitle == "" || maintitle == null) this.sharemaintitle = '易卖通首届线上家博会';
				if (content == "" || content == null) this.sharetitle = '海量新品爆品,产品物流空前优惠,不容错过';
				if (shareimg == "" || shareimg == null) this.shareimage = 'https://jbh.4coder.org.cn/web/share_img.jpg';
				this.$comm.setShare({
					title: this.sharemaintitle,
					desc: this.sharetitle,
					imgUrl: this.shareimage,
					link: this.url,
				})
			},
			set_config(activity_id) {
				this.$comm.request({
					url: 'api/activity_index',
					method: 'GET',
					data: {
						activity_id: this.activity_id,
					},
					success: res => {
						console.log(res,'api/activity_index')
						let share_url = this.$comm.app_config.share_url + '/factory/sign?activity_id=' + this.activity_id;
						this.url = share_url;
						this.activity_name = res.data.result.title
						// this.company_name = res.data.result.staff_info.fac_name;
						this.sharemaintitle = res.data.result.share_title
						this.sharetitle = res.data.result.share_content
						this.shareimage = res.data.result.share_img
						this.share()
					}
				});
			},

			//获取商家列表
			factory_list(activity_id, index) {
				if (this.status == 'nomore') {
					return
				}
				this.$comm.request({
					url: 'api/factory_list',
					method: 'GET',
					//通过ID查询数据，先传ID
					data: {
						activity_id: this.activity_id,
						page: this.page,
					},
					success: res => {
						console.log(res, 'factory_list');
						// this.factory_list_data = res.data.result.list;
						let list = res.data.result.list
						this.factory_list_data = this.factory_list_data.concat(list)
						if (list.length < 10) {
							this.status = 'nomore'
						}
					}
				});
			},

			//获取可视区域高度
			get_scrollH() {
				uni.getSystemInfo({
					success: (res) => {
						this.scrollH = res.windowHeight - uni.upx2px(270)
						// console.log(res.windowHeight, '可视区域高度',this.scrollH)
					}
				})
			},

			loaddata() {
				this.page = 1
				if (this.need_pay == 1) {
					this.status = 'more'
					this.factory_list_data = []
					this.factory_list()
				} else {
					if (this.admintabShow == true) {
						this.status = 'more'
						this.factory_list_data = []
						this.factory_list()
					} else if (this.clienttapShow == true) {
						this.clientstatus = 'more'
						this.client_list = []
						this.get_invite_check_list()
					}
				}
				setTimeout(function() {
					uni.stopPullDownRefresh();
				}, 1000);
			},

			//打开邀请二维码
			openyaoqing(Class, Position) {
				this.$nextTick(function() {
					this.AlertClass = Class;
					this.AlertPosition = Position;
					this.$nextTick(function() {
						this.make();
						uni.showToast({
							icon: 'loading',
							duration: 450,
							title: '二维码生成中...',
						});
						setTimeout(() => {
							this.$refs.openAlertyaoqing.Show();
						}, 500);
					});
				});
			},

			//制作二维码
			make(id) {
				var id = this.activity_id
				let title = this.activity_name
				let rukou = '商家报名入口'
				uQRCode.make({
					canvasId: 'qrcode',
					componentInstance: this,
					text: this.$comm.app_config.share_url + '/factory/sign?activity_id=' + id,
					// text: 'http://192.168.0.190:8080/bm/#/pages/factory/sign?activity_id=' + id,
					size: 230,
					backgroundColor: '#FFFFFF',
					foregroundColor: '#000000',
					fileType: 'jpg',
					correctLevel: uQRCode.defaults.correctLevel,
					success: res => {
						// console.log(id, '二维码显示当前的活动ID');
						postMaker(66, [title, res, rukou], 500, url => {
							this.haibao_url = url;
						});
					}
				});
			},
			
			admintab() {
				this.admintabShow = true
				this.clienttapShow = false
				this.clienticon = '../../static/client.png'
				this.adminicon = '../../static/admin1.png'
			},
			
			clienttab() {
				this.admintabShow = false
				this.clienttapShow = true
				this.clienticon = '../../static/client1.png'
				this.adminicon = '../../static/admin.png'
			},
			
			//回到顶部
			top() {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 300
				});
			},
			onPageScroll(e) { //根据距离顶部距离是否显示回到顶部按钮
				if (e.scrollTop > 600) { //当距离大于600时显示回到顶部按钮
					this.flag = true
				} else { //当距离小于600时显示回到顶部按钮
					this.flag = false
				}
			},
		}
	};
</script>

<style lang="scss" scoped>
	@import '@/common/main.css';
	@import  '@/common/zcm-main.css';
	
	page {
		font-size: 30rpx;
		background-color: #fff;
	}
	
	.clienttext {
		padding: 0 30rpx; color: #f00; margin: 30rpx 0 10rpx 0; line-height: 50rpx;
	}
	
	.staff {
		padding: 0 50rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		// margin-left: 50rpx;
		margin-top: 10rpx;
	
		.staff-em {
			height: 35rpx;
			width: 10rpx;
			background-color: #475dfc;
			border-radius: 50rpx;
		}
	
		.staff-text {
			font-weight: bold;
			margin-left: 10rpx;
		}
	}
	
	@mixin pass1 {
		width: 120rpx;
		// margin-left: 50rpx;
		font-size: 25rpx;
		color: #ffffff;
		padding: 6rpx 10rpx 6rpx 10rpx;
		text-align: center;
		border-radius: 50rpx;
	}
	
	.no-pass {
		@include pass1;
		background-color: #A40000;
	}
	
	.pass {
		@include pass1;
		background-color: #5fb256;
	}
	
	.bottom-box {
		display: flex; align-items: center; justify-content: space-around; position: fixed; bottom: 0; width: 750rpx; background-color: #fff; height: 100rpx; border-top: 1px solid #eee; font-size: 24rpx;
		
		.admin-box {
			display: flex;flex-direction: column; align-items: center;
			
			.admin-img {
				height: 60rpx; width: 60rpx;
			}
		}
	}

	.head {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 30rpx 0 0 0;
		//水平居中
		// position: fixed;
		// left: 0;
		// right: 0;
		// margin: 0 auto;
	}

	.headtext {
		color: #ffffff;
		line-height: 2.5;
		background-color: #475dfc;
		width: 400rpx;
		text-align: center;
		border-radius: 50rpx;
	}

	@mixin xuanxiangka {
		width: 50%;
		text-align: center;
		padding-top: 15rpx;
		padding-bottom: 10rpx;
		font-size: 30rpx;
		margin-top: 20rpx;
		margin-bottom: 20rpx;
	}

	.css {
		@include xuanxiangka;
		color: #fff;
		background-color: #475dfc;
		border-radius: 30rpx 30rpx 0 0;
	}

	.css1 {
		@include xuanxiangka;
		border-bottom: solid 5rpx #475dfc;
	}

	.canvas {
		// background-color: #4CD964;
		width: 500rpx;
		height: 500rpx;
		position: fixed;
		right: 100vw;
		bottom: 100vh;
		z-index: -9999;
		opacity: 0;
	}

	.sharetext {
		color: #fff;
		font-size: 35rpx;
		margin-bottom: 10rpx;
	}

	.shareimg {
		border-radius: 20rpx;
		height: 950rpx;
		width: 580rpx;
	}
	
	.top {
		position: relative;
		display: none;
		/* 先将元素隐藏 */
	}
	
	.topc {
		position: fixed;
		right: 0;
		// background: #F0F0F0;
		bottom: 120rpx;
		right: 30rpx;
		background-color: #FFF;
		border: solid 2rpx #475dfc;
		border-radius: 100rpx;
		height: 80rpx;
		width: 80rpx;
		line-height: 80rpx;
	}
</style>
